package com.peanut.baby.mvp.sign;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.http.HttpConfig;
import com.peanut.baby.model.DailySign;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.point.PointRecordActivity;
import com.peanut.baby.mvp.web.WebViewActivity;
import com.peanut.devlibrary.util.SysUtil;
import com.peanut.devlibrary.widget.TitleLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class SignInActivity extends BaseMvpActivity
        implements TitleLayout.OnTitleClickListener,
        View.OnClickListener, SignContract.View {


    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.point_record)
    TextView pointRecord;
    @BindView(R.id.week_container)
    LinearLayout weekContainer;
    @BindView(R.id.total_point)
    TextView totalPoint;
    @BindView(R.id.days)
    TextView days;
    @BindView(R.id.tomorrow_point)
    TextView tomorrowPoint;

    private SignPresenter presenter;

    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, SignInActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        ButterKnife.bind(this);
        presenter = new SignPresenter(this,this);
        initViews();
    }

    private void initViews() {
        fixWeeklyPointSize();
        title.setOnTitleClickedListener(this);
        title.setTitle("签到");
        title.setTitleRightText("积分规则");
        title.setTitleRightVisibility(true);

        pointRecord.setOnClickListener(this);

//        DailySign cache = DBManager.getInstance().getSignRecord(Integer.parseInt(InitManager.getInstance().getUser().userId), TimeUtil.getCurrentDate());
//        if (cache != null) {
//            onDataGet(cache);
//            return;
//        }

        presenter.requestData();
    }

    private void fixWeeklyPointSize() {
        int pointSize = (InitManager.getInstance().getScreenWidth() - SysUtil.dip2px(20) - SysUtil.dip2px(15) * 6) / 7;
        int childCount = weekContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = weekContainer.getChildAt(i);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams();
            params.width = pointSize;
            params.height = pointSize;
            child.setLayoutParams(params);
        }
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {
//        PointRulesActivity.start(this);
        WebViewActivity.start(this, HttpConfig.POINT_RULE_URL,"积分规则","");
    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.point_record:
                PointRecordActivity.start(this);
                break;
        }
    }

    @Override
    public void onDataGet(DailySign sign) {
        updateUIDisplay(sign);
    }

    private void updateUIDisplay(DailySign sign) {
        days.setText(""+sign.xDays);
        int day = sign.xDays % 7;
        int tomorrowViewIndex = day;
        TextView tomorrowView = (TextView) weekContainer.getChildAt(tomorrowViewIndex);
        tomorrowPoint.setText("+"+tomorrowView.getText());
        totalPoint.setText(sign.totalPoint+"");

        TextView _item = (TextView) weekContainer.getChildAt(day == 0 ? 6:day - 1);
        _item.setEnabled(true);
   }

    @Override
    public void onRequestFailed(String reason) {

    }
}
